package com.qdlc.p2p.biz.task.impl;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.security.Security;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.Properties;

import javax.mail.Address;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

import com.qdlc.p2p.biz.util.mail.Mail;
import com.qdlc.p2p.common.util.DateUtil;

/**
 * 发送邮件
 * Created by lenovo on 2016/1/5.
 */
public class MailUtil {

    public static List<String> receiveAddress = new ArrayList<String>();

    static  {
        Properties pro = new Properties();
        try {
            InputStream inStr = MailUtil.class.getResourceAsStream("/mailReceive.properties");
            pro.load(inStr);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        Enumeration enu = pro.elements();
        while (enu.hasMoreElements()) {
            String key = (String) enu.nextElement();
            receiveAddress.add(key);
        }
    }

    public static void sendMail(String title,Object content) {
        Mail mail = Mail.getInstance();
        try {
            Properties props = new Properties();
            props.put("mail.smtp.host", mail.getHost());
            props.put("mail.smtp.auth", "true");
            Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
            props.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
            props.setProperty("mail.smtp.port", mail.getPort());
            props.setProperty("mail.smtp.socketFactory.port", mail.getPort());

            Session session = Session.getDefaultInstance(props, mail.getAuth());
            MimeMessage message = new MimeMessage(session);
            message.setContent("Hello", "text/plain");
            message.setSubject(title, "utf-8");// 设置邮件主题
            message.setSentDate(DateUtil.getNow());// 设置邮件发送时期
            Address address = new InternetAddress(mail.getFrom(), mail.getNick(), "utf-8");

            message.setFrom(address);// 设置邮件发送者的地址
            for (String receive : receiveAddress) {
                Address toaddress = new InternetAddress(receive);// 设置邮件接收者的地址
                message.addRecipient(Message.RecipientType.TO, toaddress);
            }
            // 创建一个包含HTML内容的MimeBodyPart
            Multipart mainPart = new MimeMultipart();
            BodyPart html = new MimeBodyPart();
            if(content instanceof String)
            {
                html.setContent(content, "text/html; charset=utf-8");
            } else if(content instanceof Exception)
            {
                StringWriter sw = new StringWriter();
                PrintWriter pw = new PrintWriter(sw);
                ((Exception)content).printStackTrace(pw);//将异常信息输入到pw(PrintWriter)中
                StringBuffer sb = sw.getBuffer();

                html.setContent(sb.toString(), "text/html; charset=utf-8");
            }
            mainPart.addBodyPart(html);
            // 将MiniMultipart对象设置为邮件内容
            message.setContent(mainPart);
            try {
                Transport.send(message);
            } catch (MessagingException e) {
            }
        } catch (Exception e) {
        }
    }
}
